function xd=diag3d(x)
% ===================================================================
% DIAG3D 
% Extract the diagonal of a 3 dimensional matrix X with dimensions 
% [m m p] where p is number of pages 
% Resulting matrix is [m p] such that each column corresponds to the
% diagonal of a page 
% Alejandro Justiniano March 7 2008 
% ===================================================================
[nr nc np]=size(x); 
if nr~=nc;error('Submatrices must be square');end
if np==1 
    xd=diag(x); 
else 
    pos=find((repmat(eye(nr),[1 1 np]))==1);  
    xd=reshape(x(pos),[nr np]);
end 